package item;

import graphics.DebugDrawable;
import graphics.Drawable;
import graphics.GaugeBar;

import java.awt.Color;
import java.awt.Graphics;
import java.io.Serializable;

import utils.Vector2d;
import bots.BotManager;
import bots.IBot;

public class BotSpawn implements Drawable, DebugDrawable, Serializable {
	BotManager botman;
	
	Vector2d coords;
	private static final float radius = 20F;
	private static final int reloadTime = 2000;
	private static final int spawnTime = 500;
	
	int team;
	int reload;
	int spawn;
	
	GaugeBar reloadBar, spawnBar;
	
	
	public BotSpawn(BotManager botman) {
		this.botman = botman;
		coords = new Vector2d();
		team = 0;
		reload = 0;
		spawn = 0;
		
		reloadBar = new GaugeBar(40, 4);
		reloadBar.setSubBarCount(1);
		reloadBar.setColor(0, Color.GREEN);
		spawnBar = new GaugeBar(40, 4);
		spawnBar.setSubBarCount(1);
		spawnBar.setColor(0, Color.MAGENTA);
	}
	
	public BotSpawn(BotManager botman, Vector2d startCoords, int team) {
		this.botman = botman;
		coords = startCoords;
		this.team = team;
		reload = 0;
		spawn = 0;
		
		reloadBar = new GaugeBar(40, 4);
		reloadBar.setNewCoords(startCoords.subtract(new Vector2d(20, 31)));
		reloadBar.setSubBarCount(1);
		reloadBar.setColor(0, Color.GREEN);
		spawnBar = new GaugeBar(40, 4);
		spawnBar.setNewCoords(startCoords.subtract(new Vector2d(20, 24)));
		spawnBar.setSubBarCount(1);
		spawnBar.setColor(0, new Color(160, 32, 240));
	}

	public BotSpawn(Vector2d startCoords, int team) {
		coords = startCoords;
		this.team = team;
		this.botman = null;
		reload = 0;
		spawn = 0;
		
		reloadBar = new GaugeBar(40, 4);
		reloadBar.setNewCoords(startCoords.subtract(new Vector2d(20, 31)));
		reloadBar.setSubBarCount(1);
		reloadBar.setColor(0, Color.GREEN);
		spawnBar = new GaugeBar(40, 4);
		spawnBar.setNewCoords(startCoords.subtract(new Vector2d(20, 24)));
		spawnBar.setSubBarCount(1);
		spawnBar.setColor(0, new Color(160, 32, 240));
	}
	
	public void setTeam(int team) {
		this.team = team;
	}
	
	public int getTeam() {
		return team;
	}
	
	public void setBotman(BotManager botman) {
		this.botman = botman;
	}
	
	public BotManager getBotman() {
		return botman;
	}
	
	public Vector2d getCoords() {
		return coords;
	}
	
	public float getRadius() {
		return radius;
	}

	public void setCoords(int x, int y) {
		coords = new Vector2d(x, y);

		reloadBar.setNewCoords(coords.subtract(new Vector2d(20, 31)));
		spawnBar.setNewCoords(coords.subtract(new Vector2d(20, 24)));
	}
	
	public boolean canSpawn() {
		return reload == reloadTime;
	}

	public void computeNextFrame() {
		if(!canSpawn()) {
			reload++;
			
			reloadBar.setAmount(0, (float)reload/(float)reloadTime);
		}
		else {
			int nbAlly = 0;
			
			for(IBot bot : botman.getBotList(team)) {
				if(coords.distance(bot.getCoords()) < radius + bot.botRadius()) {
					nbAlly++;
				}
			}
			
			if(nbAlly == 0) {
				if(spawn > 0) {
					spawn -= 2;
					
					if(spawn < 0)
						spawn = 0;
					
					spawnBar.setAmount(0, (float)spawn/(float)spawnTime);
				}
			}
			else {
				if(spawn <= spawnTime) {
					spawn += nbAlly;
					spawnBar.setAmount(0, (float)spawn/(float)spawnTime);
				}
				else {
					botman.createUnicornBot(coords, team);
					
					reloadBar.setAmount(0, 0F);
					spawnBar.setAmount(0, 0F);
					
					reload = spawn = 0;
				}
			}
		}
	}

	public void draw(Graphics g) {
		g.setColor(botman.getTeamColor(team));
		g.fillOval((int)coords.x - (int)(radius), (int)coords.y - (int)(radius), (int)radius * 2, (int)radius * 2);
		g.setColor(Color.BLACK);
		g.drawOval((int)coords.x - (int)(radius), (int)coords.y - (int)(radius), (int)radius * 2, (int)radius * 2);
		
		reloadBar.draw(g);
		if(spawn > 0)
			spawnBar.draw(g);
	}

	public void drawDebug(Graphics g) {
		g.setColor(Color.PINK);
		g.fillOval((int)coords.x - 2, (int)coords.y - 2, 4, 4);
		g.drawOval((int)coords.x - (int)radius, (int)coords.y - (int)radius, (int)radius * 2, (int)radius * 2);
	}

}
